import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class SendEmail3 {
    //javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
    //nested exception is:
    //java.net.ConnectException: Connection refused: connect
    //要求用户名，密码，以及其他邮箱验证信息
    public static void main(String[] args) {
        try {
            // 收件人电子邮箱
            //String to = "582956590@qq.com";
            String[] to = new String[]{"1592190338@qq.com", "543593558qq.com"};
            // 发件人电子邮箱
            String from = "3365582964@qq.com";

            // 指定发送邮件的主机为 localhost
            //String host = "localhost";
            // 指定发送邮件的主机为 smtp.qq.com
            String host = "smtp.qq.com";  //QQ 邮件服务器

            // 获取系统属性
            Properties properties = System.getProperties();

            // 设置邮件服务器
            properties.setProperty("mail.smtp.host", host);

            //如果需要提供用户名和密码给e-mail服务器来达到用户认证的目的，你可以通过如下设置来完成
            //properties.put("mail.smtp.auth", "true");
            //properties.setProperty("mail.user", "yourUser");
            //properties.setProperty("mail.password", "yourPwd");


            properties.put("mail.smtp.auth", "true");

            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);
            // 获取默认session对象
            //Session session = Session.getDefaultInstance(properties);
            //需要授权码的话//发件人邮件用户名、授权码
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            //return super.getPasswordAuthentication();
                            return new PasswordAuthentication(from, "tuptvaemqlsidbad"); //发件人邮件用户名、授权码
                        }
                    }

            );

            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            //发送一封e-mail给多个收件人，那么使用下面的方法来指定多个收件人ID：
            //void addRecipients(Message.RecipientType type,Address[] addresses)
            //后面的参数用数组
            //type:要被设置为 TO, CC 或者 BCC，这里 CC 代表抄送、BCC 代表秘密抄送
            //message.addRecipient(Message.RecipientType.TO,
            //        new InternetAddress[to]);
            InternetAddress[] sendTo = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {

                sendTo[i] = new InternetAddress(to[i]);
            }

            message.addRecipients(Message.RecipientType.TO, sendTo);

//设置在发送给收信人之前给自己(发送方)抄送一份，不然会被当成垃圾邮件，报554错
            message.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(from));
            // Set Subject: 头部头字段
            message.setSubject("This is the Subject Line!");
// 设置消息体
            message.setText("This is actual message");

            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException | GeneralSecurityException mex) {
            mex.printStackTrace();
        }


    }
}
